import Cookies from "js-cookie";

export function getToken() {
    const token = Cookies.get("token");
    if (token) return `Bearer ${token}`;
    else return null;
}

/**
 * cookie保存token
 * @param {*} payload token
 * @param {*} hour 有效时间(小时数)
 */
export function setToken(payload: string, hour = 8) {
    const timestamp: number = new Date().getTime() + hour * 60 * 60 * 1000;
    const expire = new Date(timestamp);
    Cookies.set("token", payload, {
        expires: expire,
    });
}

// 移除token
export function removeToken() {
    Cookies.remove("token");
}
